import { changeStatus, deleteOrder } from '@/api/order/index.js'
export default {
  data() {
    return {}
  },
  methods: {
    // 批量修改订单状态
    handleChangeOk() {
      const list = this.ids
      if (list.length === 0) {
        this.$message({
          message: '请至少选择一条数据',
          type: 'error'
        })
        return
      }
      const params = {
        idList: this.ids.map(o => o.id),
        status: this.changeStatusValue
      }
      changeStatus(params).then(() => {
        this.$message({
          message: '修改成功',
          type: 'success'
        })
        this.getList()
        this.changeStatusModal = false
      })
    },
    deleteOrder() {
      if (this.ids.length > 0) {
        const ids = this.ids.map(o => o.id)
        this.$confirm('确认删除此订单吗?', this.$t('common.reminder'), {
          confirmButtonText: this.$t('common.confirm'),
          cancelButtonText: this.$t('common.cancel'),
          type: 'warning'
        })
          .then(() => {
            deleteOrder(ids)
              .then(res => {
                console.log(res)
                if (res.code === 200) {
                  this.$message({
                    type: 'success',
                    message: '删除成功'
                  })
                  this.getList()
                } else {
                  this.$message({
                    type: 'errro',
                    message: res.msg || '网络错误，请稍后再试'
                  })
                }
              })
              .catch(err => {
                this.$message({
                  type: 'errro',
                  message: err || '网络错误，请稍后再试'
                })
              })
          })
          .catch(() => { })
      } else {
        this.$message({
          message: '请先选择数据',
          type: 'error'
        })
      }
    },
    // 收货
    receiveShop() {
      const list = this.ids
      if (list.length === 0) {
        this.$message({
          message: '请至少选择一条数据',
          type: 'error'
        })
        return
      }
      const params = {
        idList: this.ids.map(o => o.id),
        status: 3
      }
      this.$confirm('确认变更此订单状态吗?', this.$t('common.reminder'), {
        confirmButtonText: this.$t('common.confirm'),
        cancelButtonText: this.$t('common.cancel'),
        type: 'warning'
      })
        .then(() => {
          changeStatus(params).then(() => {
            this.$message({
              message: '修改成功',
              type: 'success'
            })
            this.getList()
          })
        })
        .catch(() => { })
    },
    // 发货
    deliverGoods() {
      const list = this.ids
      if (list.length === 0) {
        this.$message({
          message: '请至少选择一条数据',
          type: 'error'
        })
        return
      } else {
        this.deliveryModal = true
      }
    },
    deliverySubmit() {
      const params = {
        idList: this.ids.map(o => o.id),
        deliveryTime: this.deliveryTime,
        status: 2
      }
      changeStatus(params).then(() => {
        this.$message({
          message: '修改成功',
          type: 'success'
        })
        this.getList()
      })
    },
    // 配货
    prepareShop() {
      const list = this.ids
      if (list.length === 0) {
        this.$message({
          message: '请至少选择一条数据',
          type: 'error'
        })
        return
      }
      const params = {
        idList: this.ids.map(o => o.id),
        status: 1
      }
      this.$confirm('确认变更此订单状态吗?', this.$t('common.reminder'), {
        confirmButtonText: this.$t('common.confirm'),
        cancelButtonText: this.$t('common.cancel'),
        type: 'warning'
      })
        .then(() => {
          changeStatus(params).then(() => {
            this.$message({
              message: '修改成功',
              type: 'success'
            })
            this.getList()
          })
        })
        .catch(() => { })
    },
    cancelOrder() {
      const list = this.ids
      if (list.length === 0) {
        this.$message({
          message: '请至少选择一条数据',
          type: 'error'
        })
        return
      }
      const params = {
        idList: this.ids.map(o => o.id),
        status: 4
      }
      changeStatus(params).then(() => {
        this.$message({
          message: '修改成功',
          type: 'success'
        })
        this.getList()
      })
    },
    changeStatus() {
      this.changeStatusModal = true
    },
    uploadExcel() {
      this.orderListModal = true
      this.fileList = []
    },
    handleUploadSuccess(res, file) {
      if (res.code === 200) {
        const fileList = this.fileList
        const o = {
          name: file.name,
          url: file.url
        }
        fileList.push(o)
        this.$message({
          type: 'success',
          message: '上传成功'
        })
      } else {
        this.fileList = []
        this.$message({
          type: 'error',
          message: res.message
        })
      }
    }
  }
}
